package com.xinghen.security.core.validate.code;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Objects;

/**
 * @author xinghen
 * @ClassName: ValidateCodeProcessorManager
 * @Package: com.xinghen.security.core.validate.code
 * @Description 校验码处理器管理器
 * @create 2018-04-18 14:40
 */
@Component
public class ValidateCodeProcessorManager {

    private final Map<String, ValidateCodeProcessor> validateCodeProcessors;

    @Autowired
    public ValidateCodeProcessorManager(Map<String, ValidateCodeProcessor> validateCodeProcessors) {
        this.validateCodeProcessors = validateCodeProcessors;
    }

    public ValidateCodeProcessor findValidateCodeProcessor(ValidateCodeType codeType) {
        return findValidateCodeProcessor(StringUtils.lowerCase(codeType.toString()));
    }

    public ValidateCodeProcessor findValidateCodeProcessor(String codeType) {
        String name = codeType.toLowerCase() + ValidateCodeProcessor.class.getSimpleName();
        ValidateCodeProcessor processor = validateCodeProcessors.get(name);
        if (Objects.isNull(processor)) {
            throw new ValidateCodeException("验证码处理器" + name + "不存在");
        }
        return processor;
    }
}
